package com.sanjiahantang.youjiarole.umengshare;

import android.Manifest;
import android.app.Activity;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import static com.sanjiahantang.youjiarole.config.ConfigValue.SHARE_IP;

/**
 * Created by yan on 2017/12/13.
 */

public class UmengShareManager {


    /**
     * @param context
     * @param url
     */
    public static void share(final Activity context, String url) {
        share(context,
                context.getResources().getString(R.string.app_share_title),
                context.getResources().getString(R.string.app_share_content),
                url);
    }
    public static void share(final Activity context, String title, String content,String url) {

            UMWeb web = new UMWeb(SHARE_IP + url);
            web.setTitle(title);//标题
            web.setThumb(new UMImage(context, R.mipmap.ic_launcher));  //缩略图
            web.setDescription(content);
            new ShareAction(context).withMedia(web)
                    .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.WEIXIN_FAVORITE)
                    .setCallback(new ShareListener(context))
                    .open();
    }

    /**
     * 共享监听类
     */
    private static class ShareListener implements UMShareListener {

        private Activity context;

        public ShareListener(Activity context) {
            this.context = context;
        }

        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            Utils.showToast(context, "分享成功");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            Utils.showToast(context, "分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            Utils.showToast(context, "取消分享");
        }
    }

}
